package com.ermace.robo.jumper.game;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import com.ermace.robo.jumper.game.Player.Position;

public class LineTest {
	
	private Line Lone;
	private Line Ltwo;
	private Line Ltree;
	private Line Lvert;
	private Line Lhor;
	private Line Lparrallel;
	private Line Lhor2;
	private Line Lvert2;

	@Before
	public void setUp() throws Exception {
		Lone = new Line(0,0,50,50);
		Ltwo = new Line(0,50,50,0);
		Ltree = new Line(20,20,70,70);
		Lparrallel = new Line(20,0,70,50);
		Lvert = new Line(100, 100, 100,150);
		Lhor = new Line(110,110, 160,110);
		
		Lhor2 = new Line(40,25, 80,25);
		Lvert2 = new Line(41, 24, 41, 65);
	}

	@Test
	public void getXCorrespondingTo_LegalCase()
	{
		assertEquals(2, Lone.getXCorrespondingTo(2), 0);
		assertEquals(70, Ltree.getXCorrespondingTo(70), 0);
		assertEquals(30, Ltwo.getXCorrespondingTo(20), 0);
		assertEquals(100, Lvert.getXCorrespondingTo(130), 0);
	}
	
	@Test
	public void getXCorrenspondingTo_OutOfBounds()
	{
		assertEquals(Double.NaN, Lone.getXCorrespondingTo(-1), 0);
		assertEquals(Double.NaN, Ltwo.getXCorrespondingTo(71), 0);
		assertEquals(Double.NaN, Ltree.getXCorrespondingTo(19), 0);
		assertEquals(Double.NaN, Lvert.getXCorrespondingTo(99), 0);
		assertEquals(Double.NaN, Lhor.getXCorrespondingTo(111), 0);
	}
	
	@Test
	public void getXCorrespondingTo_HorizontalCase() {
		assertEquals(135, Lhor.getXCorrespondingTo(110), 0);
	}
	
	@Test
	public void getYCorrespondingTo_LegalCase()
	{
		assertEquals(2, Lone.getYCorrespondingTo(2), 0);
		assertEquals(70, Ltree.getYCorrespondingTo(70), 0);
		assertEquals(30, Ltwo.getYCorrespondingTo(20), 0);
		assertEquals(110, Lhor.getYCorrespondingTo(130), 0);
	}
	
	@Test
	public void getYCorrenspondingTo_OutOfBounds()
	{
		assertEquals(Double.NaN, Lone.getYCorrespondingTo(-1), 0);
		assertEquals(Double.NaN, Lone.getYCorrespondingTo(110), 0);
		assertEquals(Double.NaN, Ltwo.getYCorrespondingTo(71), 0);
		assertEquals(Double.NaN, Ltree.getYCorrespondingTo(19), 0);
		assertEquals(Double.NaN, Lvert.getYCorrespondingTo(99), 0);
		assertEquals(Double.NaN, Lhor.getYCorrespondingTo(109), 0);
	}
	
	@Test
	public void getYCorrespondingTo_VerticalCase() {
		assertEquals(125, Lvert.getYCorrespondingTo(100), 0);
	}

	@Test
	public void intersection_LegalCase() {		
		assertEquals(new Position(25, 25), Lone.intersection(Ltwo));
		assertEquals(new Position(25, 25), Ltwo.intersection(Lone));

		assertEquals(new Position(35, 15), Lparrallel.intersection(Ltwo));
		assertEquals(new Position(35, 15), Ltwo.intersection(Lparrallel));

		assertEquals(new Position(41, 25), Lhor2.intersection(Lvert2));
		assertEquals(new Position(41, 25), Lvert2.intersection(Lhor2));
	}

	@Test
	public void intersection_OutOfBounds() {		
		assertEquals(null, Lone.intersection(Lhor));
		assertEquals(null, Lhor.intersection(Lone));

		assertEquals(null, Lparrallel.intersection(Lvert));
		assertEquals(null, Lvert.intersection(Lparrallel));

		assertEquals(null, Lhor2.intersection(Lone));
		assertEquals(null, Lone.intersection(Lhor2));
	}
	
	@Test
	public void intersection_Parrallel() {
		assertEquals(null, Lone.intersection(Lparrallel));
		assertEquals(null, Lparrallel.intersection(Lone));
	}
}
